﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow2211A.Domain.Entities;
using WorkflowCore.Persistence.EntityFramework.Models;
using WorkflowCore.Persistence.EntityFramework.Services;

namespace Workflow2211A.Domain
{
    public class Workflow2211Context : WorkflowDbContext
    {
        public DbSet<PersistedEvent> Event { get; set; }   
        public DbSet<PersistedExecutionError> ExecutionError { get; set; }   
        public DbSet<PersistedExecutionPointer> ExecutionPointer { get; set; }   
        public DbSet<PersistedExtensionAttribute> ExtensionAttribute { get; set; }   
        public DbSet<PersistedScheduledCommand> ScheduledCommand { get; set; }   
        public DbSet<PersistedSubscription> Subscription { get; set; }   
        public DbSet<PersistedWorkflow> Workflow { get; set; }   
        public DbSet<WorkflowDefinitionModel> Definition { get; set; }   
        public DbSet<AuditModel> Audit { get; set; }
        public DbSet<UserModel> User { get; set; }
        public DbSet<RoleModel> Role { get; set; }
        //public DbSet<FormTemplateModel> FormTemplate { get; set; }
        public DbSet<WorkflowInstanceModel> WorkflowInstance { get; set; }

        //Program 中添加配置可能会报错，所以挪到这里进行配置连接字符串
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql("Server=101.201.170.32;port=3306;uid=root;pwd=xzy20050504;database=workflow3;sslMode=None", new MySqlServerVersion("5.7"));
        }

        protected override void ConfigureEventStorage(EntityTypeBuilder<PersistedEvent> builder)
        {
            builder.ToTable("Event");
        }

        protected override void ConfigureExecutionErrorStorage(EntityTypeBuilder<PersistedExecutionError> builder)
        {
            builder.ToTable("ExecutionError");

        }

        protected override void ConfigureExecutionPointerStorage(EntityTypeBuilder<PersistedExecutionPointer> builder)
        {
            builder.ToTable("ExecutionPointer");

        }

        protected override void ConfigureExetensionAttributeStorage(EntityTypeBuilder<PersistedExtensionAttribute> builder)
        {
            builder.ToTable("ExtensionAttribute");

        }

        protected override void ConfigureScheduledCommandStorage(EntityTypeBuilder<PersistedScheduledCommand> builder)
        {
            builder.ToTable("ScheduledCommand");

        }

        protected override void ConfigureSubscriptionStorage(EntityTypeBuilder<PersistedSubscription> builder)
        {
            builder.ToTable("Subscription");

        }

        protected override void ConfigureWorkflowStorage(EntityTypeBuilder<PersistedWorkflow> builder)
        {
            builder.ToTable("Workflow");

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //统一配置软删除过滤
            modelBuilder.Entity<WorkflowDefinitionModel>().HasQueryFilter(x=>!x.IsDeleted);
            modelBuilder.Entity<AuditModel>().HasQueryFilter(x=>!x.IsDeleted);
        }
    }
}
